# -*- coding:utf-8 -*-
from flask import request, jsonify, abort
from flask_cors import CORS

from com.zzh.dao.messageDao import MessageAPI
import time

from . import app

CORS(app, supports_credentials=True)

local_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))

'''消息盒子表 zzh_message API'''
__author__ = 'YM'


@app.route('/query-message', methods=['POST'])
def queryMessage():
    messageType = request.json.get('messageType', "")
    userId = request.json.get('userId', '')

    messageInfo = MessageAPI().queryMessage(messageType,userId)
    if messageInfo:
        return jsonify({"messageListJson": messageInfo, "massage": {"code": 1, "info": "queryMessage OK"}})
    else:
        return jsonify({"message": {"code": 0, "info": "当前用户没有消息"}})


@app.route('/add-message', methods=['POST'])
def addMessage():
    if not request.json:
        print request.json
        abort(400)
    addMessage = {
        'messageType': request.json.get('messageType', ""),  # messageType  消息类型，两种情况：1.“系统消息”；2.“个人提示消息”
        'messageName': request.json.get('messageName', ""),  # messageName  消息接收人名称
        'messageTime': request.json.get('messageTime', local_time),  # messageTime  消息时间，默认读取调用的时候的系统时间
        'messageTitle': request.json.get('messageTitle', ""),  # messageTitle  消息标题
        'messageContent': request.json.get('messageContent', ""),  # messageContent  消息内容
        'userId': request.json.get('userId','')
    }
    addMessageResult = MessageAPI().addMessage(addMessage)
    if addMessageResult:
        return jsonify({"addMessageInfo": addMessage,
                        "massage": {"code": 1, "info": "addMessage OK"}})  # 添加函数调用成功，返回添加的信息（不包括message_id）
    else:
        return jsonify({"message": {"code": 0, "info": "addMessage Failed"}})  # 添加函数调用失败


@app.route('/delete-message/<int:messageId>', methods=['DELETE'])  # 根据invoiceId删除invoice表指定记录
def deleteMessage(messageId):
    deleteMessageResult = MessageAPI().deleteMessageById(messageId)
    if deleteMessageResult:
        return jsonify({"message": {"code": 1, "info": "删除message操作成功！"}})
    else:
        return jsonify({"message": {"code": 0, "info": "删除message操作失败！"}})
